package cn.edu.tjnu.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.edu.tjnu.dao.IUserInfoDao;
import cn.edu.tjnu.orm.UsersBean;

public class UserInfoDaoImpl implements IUserInfoDao {

	/**
	 * 需要使用spring的依赖注入 sessionFactory对象
	 */
	private SessionFactory sessionFactory;

	@Override
	public int saveUserInfo(UsersBean userB) {
		// 保存
		Session session = sessionFactory.getCurrentSession();
		// 使用hibernate保存实体对象的数据
		Serializable si = session.save(userB);
		return Integer.valueOf(si.toString());
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<UsersBean> getAllUserInfo(int orderById) {
		// 查询所有的用户信息
		// 保存
		Session session = sessionFactory.getCurrentSession();
		// 进行查询的hql语句
		String strHQL = "from UsersBean ORDER BY userId ";
		//倒序
		if(orderById == 0){
			strHQL = "from UsersBean ORDER BY userId desc ";
		}
		// 1、获取query 对象
		Query query = session.createQuery(strHQL);
		
		// 2、获取查询结果
		List<UsersBean> lstUsers = query.list();
		return lstUsers;
	}

}
